package qn.easywechat.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qn.web.qnwebbase.common.vo.ReturnResult;
import org.springframework.stereotype.Service;
import qn.common.utils.IdGenerator;
import qn.easywechat.dao.entity.SysConfig;
import qn.easywechat.dao.mapper.SysConfigMapper;
import qn.easywechat.dao.mapper.ext.SysConfigMapperExt;
import qn.easywechat.service.ISysConigService;
import qn.easywechat.web.vo.request.GetSysConfigListRequest;
import qn.easywechat.web.vo.request.GetSysConfigRequest;
import qn.easywechat.web.vo.response.SysConfigListResponse;
import qn.easywechat.web.vo.response.SysConfigVo;
import qn.common.utils.MapperUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class SysConfigServiceImpl implements ISysConigService {

    @Resource
    SysConfigMapperExt sysConfigMapperExt;
    @Resource
    SysConfigMapper sysConfigMapper;

    @Resource
    IdGenerator idGenerator;

    @Resource
    MapperUtils mapperUtils;

    @Override
    public SysConfigListResponse getConfigList(GetSysConfigListRequest request) {
        SysConfigListResponse response = new SysConfigListResponse();

        HashMap<String, Object> para = new HashMap<>();
        para.put("code", request.getCode());
        para.put("typeName", request.getTypeName());
        para.put("typeName", request.getCreateTimeStart());

        int pageNum = request.getPageNo() < 1 ? 1 : request.getPageNo();
        int pageSize = request.getPageSize() < 1 ? 20 : request.getPageSize();
        PageHelper.startPage(pageNum, pageSize);

        List<SysConfig> list = sysConfigMapperExt.findSysConfigList(para);
        PageInfo<SysConfig> pageInfo = new PageInfo<SysConfig>(list);
        response.setList(mapperUtils.map(pageInfo.getList(), SysConfigVo.class));

        qn.easywechat.web.vo.common.PageInfo pageInfoData = new qn.easywechat.web.vo.common.PageInfo();
        pageInfoData.setPageNo(pageInfo.getPageNum());
        pageInfoData.setPageSize(pageInfo.getPageSize());
        pageInfoData.setTotal(pageInfo.getTotal());
        response.setPageInfo(pageInfoData);

        return response;
    }

    public SysConfigVo getSysConfig(GetSysConfigRequest request) {
        SysConfigVo vo = null;

        Example example = new Example(SysConfig.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("code", request.getCode());

        List<SysConfig> entitys = sysConfigMapper.selectByExample(example);
        if (entitys != null && entitys.size() > 0) {
            vo = mapperUtils.map(entitys.get(0), SysConfigVo.class);
        }


        return vo;
    }

    public ReturnResult addSysConfig(SysConfigVo vo){
        ReturnResult res =new ReturnResult();
        SysConfig entity = mapperUtils.map(vo,SysConfig.class);
        entity.setCode(idGenerator.GenCode());
        entity.setId(idGenerator.GenId());
        entity.setCreatetime(new Date());
        sysConfigMapper.insert(entity);


        entity.setId(idGenerator.GenId());
        entity.setCode("new"+ entity.getCode());
        sysConfigMapper.insert(entity);

        return res;
    }
}
